package file;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 遍历指定目录，找到名字中含有关键字的文件并选择是否删除
 * User: 王圆豪
 * Date: 2025-05-12
 * Time: 19:43
 */
public class Demo14 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入指定目录：");
        String s1 = sc.next();
        System.out.println("请输入关键字：");
        String s2 = sc.next();

        //new出File对象并查看是否合法
        File file = new File(s1);
        if (!file.isDirectory()) {
            System.out.println("指定目录不存在！");
            return;
        }

        //遍历目录
        func1(file, s2);

        sc.close();
    }

    private static void func1(File file, String s2) {
        File[] files = file.listFiles();
        if (files == null) return;
        for (File tmp : files){
            if (tmp.isDirectory()){
                func1(tmp, s2);
            }else{
                dealFile(tmp);
            }
        }
    }

    private static void dealFile(File tmp) {
        while (true){
            System.out.println("是否删除文件（Y/N）:");
            Scanner scanner = new Scanner(System.in);
            String s = scanner.next();
            if (!s.equals("Y") && !s.equals("N")){
                System.out.println("输入非法！");
            }else if (s.equals("Y")){
                tmp.delete();
                System.out.println("文件已删除！");
            }
        }
    }
}
